using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BloodBot
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StateManager : Microsoft.Xna.Framework.GameComponent
    {

        Stack<BaseState> stateStack;

        MainMenuState mainMenuState;
        PlayingState playingState;
        GameOverState gameOverState;
        LungLabState lungLabState;

        public SpriteBatch spriteBatch
        {
            get;
            set;
        }

        public GameStatistics gameStats
        {
            get;
            set;
        }

        public StateManager(Game game, SpriteBatch spriteBatch, GameStatistics gameStats)
            : base(game)
        {
            // TODO: Construct any child components here
            this.spriteBatch = spriteBatch;
            this.gameStats = gameStats;
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            stateStack = new Stack<BaseState>();
            stateStack.Push(new PlayingState(this));
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (stateStack == null)
            {
                stateStack = new Stack<BaseState>();
                stateStack.Push(new MainMenuState(this));
            }
            stateStack.Peek().Update(gameTime);
            base.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            if (stateStack == null)
            {
                throw new Exception();
            }
            stateStack.Peek().Draw(gameTime);
        }

        public void GoToPlayingState(int level = 1, int enemyCount = 3)
        {
            stateStack.Push(new PlayingState(this, level, enemyCount));
        }

        public void GoToGameOverState(int score)
        {
            //would usually of course push the new GameOverState, but tried the MainMenuState and got the same spriteBatch error
            stateStack.Push(new GameOverState(this, score));            
        }

        public void GoToLungLabState(int level, int enemyCount)
        {
            stateStack.Push(new LungLabState(this, level, enemyCount));
        }
    }
}
